package cn.luyou.admin.service.impl;

import cn.luyou.admin.entity.ExamPaperQuestion;
import cn.luyou.admin.mapper.ExamPaperQuestionMapper;
import cn.luyou.admin.service.ExamPaperQuestionService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ExamPaperQuestionServiceImpl extends ServiceImpl<ExamPaperQuestionMapper, ExamPaperQuestion> implements ExamPaperQuestionService {

    @Override
    @Transactional
    public void savePaperQuestions(Long paperId, List<ExamPaperQuestion> questions) {
        // 先删除原有的题目
        deletePaperQuestions(paperId);
        
        // 保存新的题目
        if (questions != null && !questions.isEmpty()) {
            this.saveBatch(questions);
        }
    }

    @Override
    public List<ExamPaperQuestion> getPaperQuestions(Long paperId) {
        return this.list(new LambdaQueryWrapper<ExamPaperQuestion>()
                .eq(ExamPaperQuestion::getPaperId, paperId)
                .orderByAsc(ExamPaperQuestion::getQuestionOrder));
    }

    @Override
    @Transactional
    public void deletePaperQuestions(Long paperId) {
        this.remove(new LambdaQueryWrapper<ExamPaperQuestion>()
                .eq(ExamPaperQuestion::getPaperId, paperId));
    }
} 